package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.nft.dao.MallGoodsLabelDao;
import com.sc.nft.dao.MallGoodsLabelItemDao;
import com.sc.nft.dto.IdRequest;
import com.sc.nft.dto.MallGoodsLabelSaveGoodsDTO;
import com.sc.nft.entity.MallGoodsLabel;
import com.sc.nft.entity.MallGoodsLabelItem;
import com.sc.nft.entity.dto.MallGoodsLabelDTO;

import com.sc.nft.entity.dto.MallGoodsLabelPageDTO;
import com.sc.nft.entity.vo.MallGoodsLabelItemPageVO;
import com.sc.nft.entity.vo.MallGoodsLabelPageVO;
import com.sc.nft.entity.vo.MallGoodsLabelVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.MallGoodsLabelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class MallGoodsLabelServiceImpl implements MallGoodsLabelService {
    private final MallGoodsLabelDao mallGoodsLabelDao;
    private final MallGoodsLabelItemDao mallGoodsLabelItemDao;

    @Override
    public void create(MallGoodsLabelDTO goodsLabelDTO) {
        goodsLabelDTO.check();
        MallGoodsLabel mallGoodsLabel = new MallGoodsLabel();
        BeanUtils.copyProperties(goodsLabelDTO,mallGoodsLabel);
        mallGoodsLabel.insert();
    }

    @Override
    public void update(MallGoodsLabelDTO goodsLabelDTO) {
        goodsLabelDTO.check();
        MallGoodsLabel getMallGoodsLabel = mallGoodsLabelDao.getById(goodsLabelDTO.getId());
        Assert.notNull(getMallGoodsLabel, () -> new GlobalRunTimeException("标签不存在"));
        BeanUtils.copyProperties(goodsLabelDTO,getMallGoodsLabel);
        getMallGoodsLabel.updateById();
    }


    /**
     * 开启
     *
     * @param id
     */
    @Override
    public void enable(Long id) {
        mallGoodsLabelDao.updateStatus(id,Boolean.TRUE);
    }

    /**
     * 关闭
     *
     * @param id
     */
    @Override
    public void disable(Long id) {
        mallGoodsLabelDao.updateStatus(id,Boolean.FALSE);
    }

    /**
     * 详情
     *
     * @param idRequest
     * @return
     */
    @Override
    public MallGoodsLabelVO details(IdRequest idRequest) {
        MallGoodsLabel getMallGoodsLabel = mallGoodsLabelDao.getById(idRequest.getId());
        Assert.notNull(getMallGoodsLabel, () -> new GlobalRunTimeException("标签不存在"));
        MallGoodsLabelVO mallGoodsLabelVO = new MallGoodsLabelVO();
        BeanUtils.copyProperties(getMallGoodsLabel,mallGoodsLabelVO);
        return mallGoodsLabelVO;
    }

    /**
     * 列表
     *
     * @param mallGoodsLabelPageDTO
     * @return
     */
    @Override
    public IPage<MallGoodsLabelPageVO> page(MallGoodsLabelPageDTO mallGoodsLabelPageDTO) {
        return mallGoodsLabelDao.pageList(mallGoodsLabelPageDTO);
    }



    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void remove(Long id) {
        MallGoodsLabel getMallGoodsLabel = mallGoodsLabelDao.getById(id);
        Assert.notNull(getMallGoodsLabel, () -> new GlobalRunTimeException("标签不存在"));
        Assert.isFalse(getMallGoodsLabel.getStatus(),()->new GlobalRunTimeException("状态不正确"));
        mallGoodsLabelDao.removeLabel(id);
    }

    /**
     * 关联商品id
     *
     * @param mallLabelId
     * @return
     */
    @Override
    public List<MallGoodsLabelItemPageVO> goodsPage(Long mallLabelId) {
        return mallGoodsLabelDao.goodsPage(mallLabelId);
    }

    /**
     * 保存关联商品
     *
     * @param saveGoodsDTO@return
     */
    @Override
    @Transactional
    public void saveGoods(MallGoodsLabelSaveGoodsDTO saveGoodsDTO) {
        MallGoodsLabel getMallGoodsLabel = mallGoodsLabelDao.getById(saveGoodsDTO.getId());
        Assert.notNull(getMallGoodsLabel, () -> new GlobalRunTimeException("标签不存在"));
        //先删除原来的
        int count = mallGoodsLabelItemDao.countByLabelId(saveGoodsDTO.getId());
        if (count > 0) {
            Assert.isTrue(mallGoodsLabelItemDao.removeByLabelId(saveGoodsDTO.getId()),()->new GlobalRunTimeException("操作失败"));
        }
        //保存新的
        List<MallGoodsLabelItem> saveItemList = saveGoodsDTO.getItemDTOList().stream().map(e -> {
            MallGoodsLabelItem mallGoodsLabelItem = new MallGoodsLabelItem();
            mallGoodsLabelItem.setMallLabelId(saveGoodsDTO.getId());
            mallGoodsLabelItem.setMallActivityId(e.getId());
            return mallGoodsLabelItem;
        }).collect(Collectors.toList());
        Assert.isTrue(mallGoodsLabelItemDao.saveBatch(saveItemList),()->new GlobalRunTimeException("保存失败"));
    }
}
